/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.managedbean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import mrp.productionPlanning.entity.MPSTableEntity;
import mrp.productionPlanning.session.MPSTableSessionBean;

/**
 *
 * @author Wei Haoyang
 */
@ManagedBean
@ViewScoped
public class CreateMPSTableManagedBean {

    private int yearName;
    @EJB
    private MPSTableSessionBean mpsTableSessionBean = new MPSTableSessionBean();

    /** Creates a new instance of CreateMPSTableManagedBean */
    public CreateMPSTableManagedBean() {
    }

    public void create(ActionEvent event) throws IOException {
        List<MPSTableEntity> mpsListA = new ArrayList<MPSTableEntity>();
        List<MPSTableEntity> mpsListB = new ArrayList<MPSTableEntity>();
        List<MPSTableEntity> mpsListC = new ArrayList<MPSTableEntity>();
        List<MPSTableEntity> mpsListD = new ArrayList<MPSTableEntity>();
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.YEAR, yearName);
        cal.set(Calendar.MONTH, 1);
        cal.set(Calendar.DATE, 1);
        Date d = cal.getTime();

        int i = 0;
        while (i < 12) {
            mpsListA.add(mpsTableSessionBean.createMPSTableA(d));
            mpsListB.add(mpsTableSessionBean.createMPSTableB(d));
            mpsListC.add(mpsTableSessionBean.createMPSTableC(d));
            mpsListD.add(mpsTableSessionBean.createMPSTableD(d));
            cal.add(Calendar.MONTH, 1);
            d = cal.getTime();
            i++;
        }

        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("mpsListA", mpsListA);
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("mpsListB", mpsListB);
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("mpsListC", mpsListC);
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("mpsListD", mpsListD);
        FacesContext.getCurrentInstance().getExternalContext().redirect("editMPSTable.xhtml");
    }

    public int getYearName() {
        return yearName;
    }

    public void setYearName(int yearName) {
        this.yearName = yearName;
    }

    public MPSTableSessionBean getMpsTableSessionBean() {
        return mpsTableSessionBean;
    }

    public void setMpsTableSessionBean(MPSTableSessionBean mpsTableSessionBean) {
        this.mpsTableSessionBean = mpsTableSessionBean;
    }
}
